package Client.ui;


import javax.swing.DefaultListModel;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JButton;
import javax.swing.SwingConstants;

import Client.Utils;
import Client.Controler.AppliManager;
import VWorld.Avatar;
import VWorld.Direction;
import VWorld.Message;

import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.HashMap;

import javax.swing.JList;

public class RoomWindow {

	private JFrame frame;
	private AppliManager appliManager;
	private JTextArea tamessageWrote;
	private JLabel numberOfTheRoom;
	private DefaultListModel modelLConnected;
	protected HashMap<String, Avatar> nameAvatar;
	private DefaultListModel modelLDisconnected;
	private JButton btnnorth;
	private JButton btnsouth;
	private JButton btnwest;
	private JButton btneast;
	private JTextArea tamessageStream;
	
	/**
	 * Create the application.
	 * @param appliManager 
	 */
	public RoomWindow(AppliManager pappliManager) {
		this.appliManager = pappliManager;
		
		initialize();
		this.displayInfoAboutRoom();
		this.frame.setVisible(true);
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		this.modelLConnected = new DefaultListModel();
		this.modelLDisconnected = new DefaultListModel();
		frame = new JFrame();
		frame.addWindowListener(new WindowAdapter() {
			@Override
			public void windowClosed(WindowEvent e) {
				appliManager.close();
				new MainWindow(appliManager);
			}
			@Override
			public void windowClosing(WindowEvent e) {
				appliManager.close();
				new MainWindow(appliManager);
			}
		});
		frame.setBounds(100, 100, 684, 553);
		//frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.getContentPane().setLayout(null);
		
		
		JLabel lblRoomN = new JLabel("Room n\u00B0");
		lblRoomN.setBounds(10, 11, 48, 14);
		frame.getContentPane().add(lblRoomN);
		
		this.numberOfTheRoom = new JLabel();
		numberOfTheRoom.setBounds(58, 11, 46, 14);
		frame.getContentPane().add(numberOfTheRoom);
		
		JLabel lblConnected = new JLabel("Connected:");
		lblConnected.setBounds(10, 36, 150, 14);
		frame.getContentPane().add(lblConnected);
		
		JLabel lblDisconnected = new JLabel("Disconnected:");
		lblDisconnected.setBounds(10, 211, 150, 14);
		frame.getContentPane().add(lblDisconnected);
		
		this.tamessageWrote = new JTextArea();
		this.tamessageWrote.setBounds(170, 273, 389, 102);
		frame.getContentPane().add(this.tamessageWrote);
		
		JButton btnSendMessage = this.getBtnSendMessage();
		frame.getContentPane().add(btnSendMessage);
		
		this.btnnorth = this.getBtnNorth();
		frame.getContentPane().add(this.btnnorth);
		
		this.btnsouth = this.getBtnSouth();
		frame.getContentPane().add(this.btnsouth);
		
		this.btnwest = this.getBtnWest();
		frame.getContentPane().add(this.btnwest);
		
		this.btneast = this.getBtnEast();
		frame.getContentPane().add(this.btneast);
		
		JLabel lblNavigatePanel = new JLabel("Navigate panel");
		lblNavigatePanel.setHorizontalAlignment(SwingConstants.CENTER);
		lblNavigatePanel.setBounds(297, 437, 89, 14);
		frame.getContentPane().add(lblNavigatePanel);
		
		this.tamessageStream = this.getTextAreaMessageStream();
		frame.getContentPane().add(tamessageStream);
		 
		   JScrollPane scrollPane = new JScrollPane(tamessageStream,
		                     JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
		                     JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
		    scrollPane.setBounds(170, 50, 488, 212);
		  
		    frame.getContentPane().add(scrollPane, null);
		
		JButton btnProfile = new JButton("Profile");
		btnProfile.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				new CreateAvatarWindow(appliManager);
			}
		});
		btnProfile.setBounds(569, 7, 89, 23);
		frame.getContentPane().add(btnProfile);
		
		JList lConnected = this.getLConnected();
		frame.getContentPane().add(lConnected);
		
		JList ldisconnected = this.getLDisconnected();
		frame.getContentPane().add(ldisconnected);
	}

	private JList getLDisconnected() {
		final JList ldisconnected = new JList(this.modelLDisconnected);
		ldisconnected.setBounds(10, 225, 150, 150);
		
		ldisconnected.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e){
				Object ob[] = ldisconnected.getSelectedValues();
				displayInfo(nameAvatar.get(ob[0]));
			}
		});
		
		return ldisconnected;
	}

	private JList getLConnected() {
		final JList lconnected = new JList(this.modelLConnected);
		lconnected.setBounds(10, 50, 150, 150);
		
		lconnected.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e){
				Object ob[] = lconnected.getSelectedValues();
				displayInfo(nameAvatar.get(ob[0]));
			}
		});

		return lconnected;
	}
	
	private void displayInfo(Avatar avatar) {
		new CreateAvatarWindow(appliManager, avatar);
	}

	private void goTo(Direction pdirection) {
		appliManager.goTo(pdirection);
		this.displayInfoAboutRoom();
	}
	
	private void displayInfoAboutRoom() {
		this.tamessageStream.setText("");
		frame.setTitle(this.appliManager.getName() + "- Room " + this.appliManager.getRoomId());
		this.numberOfTheRoom.setText(new Integer(this.appliManager.getRoomId()).toString());
		this.btneast.setEnabled(false);
		this.btnnorth.setEnabled(false);
		this.btnsouth.setEnabled(false);
		this.btnwest.setEnabled(false);
		Direction[] dir = this.appliManager.getAvailablesDirections();
		for(Direction d : dir){
			switch (d.value()) {
			case Direction._NORD:
				this.btnnorth.setEnabled(true);
				break;
				
			case Direction._SOUTH:
				this.btnsouth.setEnabled(true);			
				break;
							
			case Direction._EAST:
				this.btneast.setEnabled(true);
				break;
				
			case Direction._WEST:
				this.btnwest.setEnabled(true);
				break;

			default:
				break;
			}
		}
		
		
		this.displayDynamicData();
	}

	private void displayDynamicData() {
		Avatar avatars[] = this.appliManager.seeAvatars();
		this.modelLConnected.removeAllElements();
		this.modelLDisconnected.removeAllElements();
		this.nameAvatar = new HashMap<String, Avatar>();
		for(Avatar av : avatars){
			this.nameAvatar.put(av.name, av);
			if(av.estConnecte){
				this.modelLConnected.addElement(av.name);
			}else{
				this.modelLDisconnected.addElement(av.name);
			}
		}
	}

	private JButton getBtnWest() {
		JButton btnwest =  new JButton("<");
		
		btnwest.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				goTo(Direction.WEST);
				tamessageWrote.setText("");
			}

		});
		btnwest.setBounds(69, 433, 89, 23);
		
		return btnwest;
	}

	private JButton getBtnSouth() {
		JButton btnsouth = new JButton("v");
		
		btnsouth.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				goTo(Direction.SOUTH);
				tamessageWrote.setText("");
			}
		});
		btnsouth.setBounds(297, 481, 89, 23);
		
		return btnsouth;
	}

	private JButton getBtnNorth() {
		JButton btnnorth = new JButton("^");
		
		btnnorth.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				goTo(Direction.NORD);
				tamessageWrote.setText("");
			}
		});
		btnnorth.setBounds(297, 386, 89, 23);
		
		return btnnorth;
	}

	private JButton getBtnEast() {
		JButton btneast = new JButton(">");
		
		btneast.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				goTo(Direction.EAST);
				tamessageWrote.setText("");
			}
		});
		btneast.setBounds(524, 433, 89, 23);
		
		return btneast;
	}

	private JButton getBtnSendMessage() {
		JButton btnSendMessage = new JButton("Send");
		
		btnSendMessage.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent arg0) {
				Message m = appliManager.sendMessageToRoom(tamessageWrote.getText());
				//refreshMessageStream();
				Utils.getClientCallbacks().newRoomMessage(m);
			}
		});
		btnSendMessage.setBounds(569, 273, 89, 102);	
		
		return btnSendMessage;
	}

	private JTextArea getTextAreaMessageStream() {
		JTextArea tamessageStream = new JTextArea();
		
		tamessageStream.setBounds(170, 50, 488, 212);
		tamessageStream.setEditable(false);
		
		return tamessageStream;
	}
	public void refreshMessageStream(Message m){
		
		this.tamessageStream.setText(this.tamessageStream.getText()+ "\n"+ m.from.name+": "+m.message);
		
		
	}
}
